﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.FeesAndFines.Search;

namespace University.Workshop.Presentation.FeesAndFines
{
    /// <summary>
    /// Interaction logic for SearchFees.xaml
    /// </summary>
    public partial class SearchFees : Page, ISearchFeesView
    {
        PresenterSearchFees pSearchFees;
        public SearchFees()
        {
            InitializeComponent();
            effectiveDateDtPk.Text = "Fecha inicio";
            pSearchFees = new PresenterSearchFees(this);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Buscar Tarifa";
        }

        

        #region Fields
        public DateTime? effectiveDate
        {
            get 
            {
                return effectiveDateDtPk.SelectedDate;
            }
        }

        public decimal charge
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(chargeTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
        }

        public decimal nonPayment
        {
            get
            {
                decimal amount;
                if (Decimal.TryParse(nonPaymentTxt.Text, out amount))
                    return amount;
                else
                    return 0;
            }
        }

        public ICollection<Model.FeesAndFines> feesFinded
        {
            get
            {
                if (FeesFindeddDgv.ItemsSource == null)
                    new List<Model.FeesAndFines>();
                return (ICollection<Model.FeesAndFines>)FeesFindeddDgv.ItemsSource;
            }
            set
            {
                FeesFindeddDgv.ItemsSource = null;
                FeesFindeddDgv.ItemsSource = value;
            }
        }
        #endregion

        #region Validations
        private void effectiveDateDtPk_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((DatePicker)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch) || ch.Equals('/'));
            }
        }

        private void effectiveDateDtPk_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            try
            {
                if (((DatePicker)sender).Text != null && ((DatePicker)sender).Text != string.Empty)
                {
                    DateTime d = DateTime.Parse(((DatePicker)sender).Text);
                }
            }
            catch
            {
                NotifyErrorMessage("Formato de fecha invalido");
            }
        }
        #endregion

        #region Buttons Actions
        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            pSearchFees.SearchInFees();
        }

        private void ShowFeeBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new FeesAndFines.ReadFees((Model.FeesAndFines)FeesFindeddDgv.SelectedItem));
        }
        #endregion

        #region ErrorMessage
        public void NotifyErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }
        #endregion

    }
}
